<?php
/**
 * @author YangLiu
 */
namespace haibao\cms\business\starpk;

use \haibao\cms\model\data\starpk\StarPkDefine as StarPkDefineModel;
use \haibao\cms\data\Brand as BrandData;
use \haibao\cms\data\Celebrity as CelebrityData;
use \haibao\cms\data\starpk\StarPkCelebrity as StarPkCelebrityData;
use haibao\cms\business\BusinessException;
use \haibao\cms\data\starpk\StarPkProduct as StarPkProductData;
use haibao\cms\data\starpk\StarPkDefine as StarPkDefineData;

class StarPkDefine extends \haibao\cms\business\Base{
    
    public static function getStarPkDefineList($starPkDefineFilter,$isGetOne = false){
        $starPkDefineData = new StarPkDefineData();
        if($isGetOne){
        	return $starPkDefineData->getOne($starPkDefineFilter);
        }else{
        	return $starPkDefineData->getAll($starPkDefineFilter);
        }
    }
    
    public static function getStarPkListById($starPkListId){
       
        $starPkDefineInfo = new StarPkDefineData();
        $starPkDefineInfo = $starPkDefineInfo->getOneById($starPkListId);
       
        $bandDataInfo = new BrandData();
        $starPkDefineInfo->StarPkBand = $bandDataInfo->getStarPkBandById($starPkDefineInfo->BrandId);
       
        $starPkCelebrityInfo = new StarPkCelebrityData();
        $starPkDefineInfo->StarPkCelebrity = $starPkCelebrityInfo->getStarPkCelebrityById($starPkListId);
        
    
        
        if($starPkDefineInfo->StarPkCelebrity){
            
            $celebrityIdsArr = array();
            foreach($starPkDefineInfo->StarPkCelebrity as $type){
                $celebrityIdsArr[] = $type->CelebrityId;
            }
            
            $celebrityDataInfo = new CelebrityData();
            $celebrityIds = $celebrityDataInfo->getCelebrityByIds($celebrityIdsArr);

          
         
            $celebrityNameCn = array();
            $celebrityNameEn = array();
            foreach($celebrityIds as $type){
                $celebrityNameCn[$type->Id] = $type->NameCn;
                $celebrityNameEn[$type->Id] = $type->NameEn;
                
            }
            
            foreach($starPkDefineInfo->StarPkCelebrity as  $key=>$celebrityName){
                $celebrityName->CelebrityNameCn = $celebrityNameCn[$celebrityName->CelebrityId];
                $celebrityName->CelebrityNameEn = $celebrityNameEn[$celebrityName->CelebrityId];
            }
            
        }
        
        $starPkProductInfo = new StarPkProductData();
        $starPkDefineInfo->StarPkProduct = $starPkProductInfo->getStarPkProductById($starPkListId);
 
        return $starPkDefineInfo;
     
    }
    public static function getStarPkDefinePublicStatus(){
        $PublicStatusArr = array(
            StarPkDefineModel::PUBLISHED_STATUS_NO,
            StarPkDefineModel::PUBLISHED_STATUS_YES,
            StarPkDefineModel::PUBLISHED_STATUS_PRE,
            StarPkDefineModel::PUBLISHED_STATUS_ING,
        );
        return $PublicStatusArr;
    }
    
    public static function addStarPkList($addStarPkModel){
		self::checkOperation(self::FUNC_CODE_STAR_PK_ADD);
       	self::checkStarPkDefineData($addStarPkModel);
       	$starPkDefineData = new StarPkDefineData();
       	$starPkDefineData->addStarPk($addStarPkModel);
     	self::saveOperateHistory(self::FUNC_CODE_STAR_PK_ADD, $addStarPkModel->toArray(true,true));
    }
    
    public static function editStarPkList($editStarPkModel){
        self::checkOperation(self::FUNC_CODE_STAR_PK_EDIT);
        self::checkStarPkDefineData($editStarPkModel);
        $starPkDefineData = new StarPkDefineData();
        $starPkDefineData->updatStarPkListById($editStarPkModel);
        self::saveOperateHistory(self::FUNC_CODE_STAR_PK_EDIT, array('id'=>$editStarPkModel->Id));
  
    }
    
    private static function checkStarPkDefineData($model){
        
        if(!$model){
            
            throw new BusinessException('error');
            
        }
        if(!$model->Title){
            
            throw new BusinessException('明星PK列表导读不能为空');
            
        }
    }
    
    public  static function delStarPkListById($id,$arrSet){
        
        self::checkOperation(self::FUNC_CODE_STAR_PK_DEL);
        
        if (!$id){
            
            throw new BusinessException('明星PKId不能为空');
        }
        $starPkDefineData = new StarPkDefineData();
        
        $starPkDefineData->delStarPkListById($id,$arrSet);
    
        self::saveOperateHistory(self::FUNC_CODE_STAR_PK_DEL, array($id,$arrSet));
        
    }
    
    
    public static function delStarPkProductById($Id){
        
        self::checkOperation(self::FUNC_CODE_STAR_PK_DEL);
        
        if (!$Id){
            throw new BusinessException('明星PK单品不能为空');
        }
        $starPkProductData = new StarPkProductData();
        
        $starPkProductData->delStarPkProductById($Id);
    
        self::saveOperateHistory(self::FUNC_CODE_STAR_PK_DEL, array($Id));
    
    }
    
    public static function updateStarPkProductNumById($starPkId){
        
        $starPkDefineData = new StarPkDefineData();
        
        $starPkDefineData->updateStarPkProductNumById($starPkId);
        
    }
    
    public static function saveProductOrder($ProductIdArr){
        
        $starPkDefineData = new StarPkDefineData();
        
        $starPKProductId = explode(';',$ProductIdArr);
        
        foreach($starPKProductId as $key => $Id){
            
            $starPkDefineData->updateProductSort($Id,$key+1);
            
        }
        
    }
    
    public static function getStarRandNum($starPkId,$isReturnTotal=true){
    	if (!$starPkId){
    		throw new BusinessException('明星PK单品不能为空');
    	}
    	$starPkDefineData = new StarPkDefineData();
    	return $starPkDefineData->getStarRandNum($starPkId,$isReturnTotal);
    }
    
    public static function getStarPkUpAndDown($starPkId){
    	if (!$starPkId){
    		throw new BusinessException('明星PK单品不能为空');
    	}
    	$starPkDefineData = new StarPkDefineData();
    	return $starPkDefineData->getStarPkUpAndDown($starPkId);
    }
    
    public static function getDataMigrateMap($tableName,$primaryKey,$isOld=false){

    	$starPkDefineData = new StarPkDefineData();
    	return $starPkDefineData->getDataMigrateMap($tableName,$primaryKey,$isOld);
    }
    public static function getPastReview($starPkId=0){

    	$starPkDefineData = new StarPkDefineData();
    	return $starPkDefineData->getPastReview($starPkId);
    }
    
}